package net.pec.geotools_svg.examples;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import net.pec.geotools_svg.svg_conversion.GenerateMapLayer;
import net.pec.geotools_svg.svg_conversion.GenerateSVGMapdocument;
import net.pec.geotools_svg.svg_conversion.PolygonElementGenerator;
import net.pec.geotools_svg.svg_conversion.PolylineElementGenerator;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;
import org.w3c.dom.Document;

public class simple_map {
	private static Document svgDocument;
	private static Transformer trans;  
	private static String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		GenerateSVGMapdocument generateSVGMapDocument = new GenerateSVGMapdocument();		
		
		
		double originX=0.0,originY=0.0,width=0.0,height=0.0,midY=0.0;
		
		File file = new File("C://development/roads.shp");
		Map map = new HashMap();
		map.put( "url", file.toURL() );
		DataStore dataStore = DataStoreFinder.getDataStore(map);
		String typeName = dataStore.getTypeNames()[0];

		FeatureSource source = dataStore.getFeatureSource(typeName);
		
		FeatureCollection countryFC = source.getFeatures();
		FeatureIterator countries = countryFC.features();
		
		originX = countryFC.getBounds().getMinX();
		originY = countryFC.getBounds().getMinY();
		width = countryFC.getBounds().getMaxX()-countryFC.getBounds().getMinX();
		height = countryFC.getBounds().getMaxY()-countryFC.getBounds().getMinY();
		
		midY = (countryFC.getBounds().getMaxY()+countryFC.getBounds().getMinY())/2;
		generateSVGMapDocument.setViewBoxAttribute(originX, originY, width, height);
		svgDocument=generateSVGMapDocument.initialiseDocument("100%","100%",  svgDocument);
		
		System.out.println(countryFC.size());
		
		GenerateMapLayer mapLayer = new GenerateMapLayer();		
		
		PolylineElementGenerator pEG = new PolylineElementGenerator();
		
		while(countries.hasNext()){
			SimpleFeature country = (SimpleFeature) countries.next();
			pEG.transformFeature(country,					 
					svgDocument,
					mapLayer,
					midY, 
					255,0,0, 
					1.0, 
					5.0,
					null);
			
		}
		
		
		svgDocument.getDocumentElement().appendChild(mapLayer.createMapLayer("map", svgDocument));
		
		/*TransformerFactory transfac = TransformerFactory.newInstance();
	     try {
	                        trans = transfac.newTransformer();
	                } catch (TransformerConfigurationException e1) {
	                        // TODO Auto-generated catch block
	                        e1.printStackTrace();
	                }
	                
	                
	     trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
	     trans.setOutputProperty(OutputKeys.INDENT, "yes");
	     
	     StringWriter sw = new StringWriter();
	     StreamResult result = new StreamResult(sw);
	     DOMSource dSource = new DOMSource(svgDocument);
	     try {
	                        trans.transform(dSource, result);
	                } catch (TransformerException e) {
	                        // TODO Auto-generated catch block
	                        e.printStackTrace();
	                }
	     String xmlString = sw.toString();*/
	     
	     //System.out.println(xmlString);
		
		JPEGTranscoder t = new JPEGTranscoder();

        // Set the transcoding hints.
        t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
                   new Float(.8));
        
       

        // Create the transcoder input.
        
        TranscoderInput input = new TranscoderInput(svgDocument);

        // Create the transcoder output.
        OutputStream ostream = new FileOutputStream("C://development/out.jpg");
        TranscoderOutput output = new TranscoderOutput(ostream);

        // Save the image.
        t.transcode(input, output);

        // Flush and close the stream.
        ostream.flush();
        ostream.close();
        System.exit(0);
		
	}

}
